<?php

/**
 * projects actions.
 *
 * @package    essayLibrary
 * @subpackage projects
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class projectsActions extends sfGraduaActions
{
    
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
    public function executeIndex($request)
    {
        $this->projects = ProjectsService::getAll();
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->projectViewTpl  = partial2Js('projects/projectView.jstpl',array(),true);
        $this->projectFormTpl  = partial2Js('projects/projectForm.jstpl',array(),true);
        $this->editDecorator   = partial2Js('globalPartials/editDecorator.jstpl',array(),true);
        $this->insertDecorator = partial2Js('globalPartials/insertDecorator.jstpl',array(),true);
        $this->listTpl         = partial2Js('globalPartials/listTpl.jstpl',array(),true);
        $this->divListItemDecorator   = partial2Js('globalPartials/divListItemDecorator.jstpl',array(),true);
        $this->setLayout('layoutTasks');
    }

    function executeClearSession() {
        session_destroy();
    }

    function executeInsert($request) {
        $data = $this->getObjRequest('project',array('use_tests'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','tests_dir','tests_result_dir','run_all_tests_dir_order'
        ));
        $this->getResponse()->setFieldIdPrefix('project');
        $id = ProjectsService::insert($data);
        $this->getResponseJsCall()
            ->addCommand('projects.list', 'refresh')
            ->addPHPParam('itemsDs', ProjectsService::getAll() )
            ->closeDialog()
            ->showFeedback('Inserido com sucesso!');
        return sfView::NONE;
    }
    
    function executeEdit($request) {
        $data = $this->getObjRequest('project',array('use_tests'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','tests_dir','tests_result_dir','run_all_tests_dir_order'
        ));
        $this->getResponse()->setFieldIdPrefix('project');
        //$('#projectForm-1 [name="project[name]"]').val('*ooo*')
        ProjectsService::edit($data);
        $this->getResponseJsCall()
            ->addCommand($jsObjName, 'update')
            ->addPHPParam('ds', ProjectsService::get($data->id))
            ->showFeedback('Sucesso na edição do Projeto!');
        if( $data->id == $this->getUser()->getAttribute('projectId', null) ) {
             AuthService::setProject($data->id);
             $this->forward('auth','showNavData');
        }
        else {
            return sfView::NONE;
        }
    }

    function executeRemove($request) {
        //Dbg::x($this->getRequest()->getParameterHolder());
        $id = $this->getRequestParameter('id');
        $jsObjName = $this->getRequestParameter('jsObjName');
        ProjectsService::delete($id);
        $this->getResponseJsCall()
            ->addCommand('projects.list', 'refresh')
            ->addPHPParam('itemsDs', ProjectsService::getAll() )
            ->showFeedback('Removido com sucesso!');
        if( $id == $this->getUser()->getAttribute('projectId', null) ) {
             AuthService::unsetProject();
             $this->forward('auth','showNavData');
        }
        else {
            return sfView::NONE;
        }
        return sfView::NONE;
    }


    function executeSetActual($request) {
        $id = $this->getRequestParameter('id');
        AuthService::setProject($id);
        $this->getResponseJsCall()->showFeedback('Project Selected');
        $this->forward('auth','showNavData');
    }


    function executeExport($request) {
        $fileName = ProjectsService::export($this->getRequestParameter('id'));
        $fileType  = 'application/json';
        header("Content-Type:".$fileType);
        header('Content-Disposition: attachment; filename="'.basename($fileName).'"');
        header("Content-Length:".filesize($fileName));
        readfile($fileName);
        die();
    }


    function executeImport($request) {
        $file   =
        GraduaUpload::doUploadFile(
            'importFile',
            $request,
            sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'imported_files',
            null,
            array('application/json')
        );
        $filePath = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'imported_files'.DIRECTORY_SEPARATOR.$file;
        $obj = new stdClass;
        $obj->id = $this->getRequestParameter('id');
        $obj->filePath = $filePath;
        ProjectsService::import($obj);
        $this->getResponseJsCall()
            ->showFeedback('Ok');
        
        return sfView::NONE;
    }

}
